<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="renderer" content="webkit|ie-comp|ie-stand" />
<meta http-equiv="X-UA-Compatible" content="IE=9; IE=8; IE=7; IE=EDGE">
<title>课程-发布课堂资源</title>

<%@ include file="/common/commonLib.jsp"%>
<link rel="stylesheet" type="text/css" href="<c:url value='/css/resourcePublish.css'/>" />
<link rel="stylesheet" type="text/css" href="<c:url value='/css/common-main.css'/>"/>
<script src="<c:url value='/js/main.js'/>" type="text/javascript" charset="utf-8"></script>
<script src="<c:url value='/js/resourcePublishClassroom.js'/>" type="text/javascript" charset="utf-8"></script>
</head>
<body>
	<div class="container-fluid main">
		<div class="row">
			<div class="col-md-12">
				<%@include file="/common/header.jsp"%>
			</div>
			<div class="col-md-12">
				<%@include file="common/courseHeader.jsp"%>
			</div>
			<div class="col-md-12">
				<div class="row">
					<div class="col-md-3">
						<%@include file="common/courseLeft.jsp"%>
					</div>
					<div class="col-md-9">
						<div class="row">
							<div class="col-md-12">
								<%@include file="common/courseNavbar.jsp"%>
							</div>
							<!-- start- 资源的下载和删除 -->
							<div class="col-md-12">
								<div class="col-md-1"></div>
								<div class="col-md-10  resource-detail">
									<ul id="resourceDetail">
										
									</ul>									
								</div>
								<div class="col-md-1"></div>
							</div>
							<!-- end-资源的下载和删除 -->			
							<!-- 发布资源到课堂按钮 -->
							<div class="col-md-12">
								<div class="col-md-1"></div>
								<div class="col-md-10">
									<input type="button" id="publish-resource-to-lesson" class="btn btn-primary" value="发布资源到课堂">
								</div>
								<div class="col-md-1"></div>
							</div>
							<!-- 课堂资源发布记录 -->
							<div class="col-md-12">
								<div class="col-md-1"></div>
								<div class="col-md-10">
									<label for="">课堂资源发布记录</label>
									<!-- 课堂资源发布记录 -->
									<ul id="resource-pub-record-lesson">
										
									</ul>
								</div>
								<div class="col-md-1"></div>
							</div>
							
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
	
	<!-- 添加资源发布的课堂 -->
	<div class="row" id="add-resource-publish-object">
		<div class="col-md-11">
			<div class="row">
				<div class="col-md-12">
					<div class="form-group">
						<div class="row">
							<div class="col-xs-2">
								<label for="">选择班级</label>
							</div>
							<div class="col-xs-4">
								<select class="form-control" id="classList" onchange="selectClass(this)">
									
								</select>
							</div>
							<div class="col-xs-6"></div>
						</div>
					</div>
				</div>
				
				<div class="col-md-12">
					<div class="form-group">
						<div class="row">
							<div class="col-xs-2">
								<label for="">选择课堂</label>
							</div>
							<div class="col-xs-10">
								<div class="publish-classroom-ready">
								
									<ul id="lessonList">
										
									</ul>
								</div>
								<div class="page-content">
									<nav class="pagination" role="navigation">
										<a class="last-page" id="lastPage">
											<i class="fa fa-angle-left"></i>
										</a>
										<span class="page-number">
											第<span id="pageNow"></span>页/共<span id="pageNum"></span>页
										</span>
										<a class="next-page" id="nextPage">
											<i class="fa fa-angle-right"></i>
										</a>
									</nav>
								</div>
							</div>
						</div>
					</div>
				</div>
				
				<div class="col-md-12">
					<div class="form-group">
						<div class="row">
							<div class="col-xs-2">
								<label for="">已选课堂</label>
							</div>
							<div class="col-xs-10">
								<div class="publish-classroom-has">
								
									<ul id="chooseList">
										
									</ul>
								</div>
							</div>
						</div>
					</div>
				</div>
				<div class="col-md-12" style="text-align:center;margin-top:2rem;">
					<button type="button" id="lessonPublishResourceBtn" class="btn btn-primary" onclick="publishResource()">发布</button>
				</div>
			</div>
		</div>
		<div class="col-md-1"></div>
	</div>
</body>
<script>
var data=${data};
var classs=[];
var lessons=[];
var chooses=[];
var courseId;
$(document).ready(function() {
	if (data.status != "success") {
		alert(data.message);
		return;
	}
	$("#resource").css("color","#00b43c");
	fetchCourse(data.data.courseId);
	courseId=data.data.courseId;
	initResourceDetail(data.data.resource,data.data.list);
	initTable(data.data);
	changeUrl(data.data.courseId);
	
})

var initResourceDetail = function(resource,list){	
	var detail=$("#resourceDetail");
	if(resource == null){
		var s = "<li>服务器出错</li>";
		detail.append(s);	
	}else{
		detail.empty();
		var s = "";
		s += "<li>";
		s += "<span class=\"resource-info\" title=\""+resource.fileName+"\">"+resource.fileName+"</span>";
		s += "<a class=\"download-url\" href=\"/CourseSupport/resource/download/"+resource.url+"\">下载</a>";
		s += "<input type=\"button\" class=\"btn btn-warning delete-resource\" onclick=\"deleteResource("+resource.id+","+list.length+","+data.data.courseId+")\" value=\"从该课程中删除\">";
		s += "</li>";
		detail.append(s);
	}	
}

var deleteResource = function(id,length,courseId){
	if(length > 0){
		layer.msg("该资源已发布到课堂，请删除所有发布后在删除该资源");
	}else{
		$.post(CONTEXT_PATH+"respository/ajaxDelete?id="+id, function(data) {
			if(data.status=="success"){
				layer.msg("删除成功！", {time:1000});
				window.location.href = CONTEXT_PATH+"respository/listData?courseId="+ courseId;
			}else{
				layer.msg(data.message);
			}
		});
	}	
}

var initTable = function(data){
	var publishList=$("#resource-pub-record-lesson");
	publishList.empty();
	if(data.list.length <= 0){
		var s = "<li>（无发布记录）</li>";
		publishList.append(s);
	}else {
		for(var i=0;i<data.list.length;i++){
			var item = data.list[i];
			var s="<li>";
			s+="<span title=\""+"班级名称："+item.className+"  上课时间："+item.beginDate+"-"+item.endDate+"\">"+"班级名称："+item.className+"  上课时间："+item.beginDate+"-"+item.endDate+"</span>";
			s+="<input type=\"button\" class=\"btn btn-success deleteBtn\" onclick=\"deleteAssign("+item.id+")\" value=\"删除\">";
			s+="</li>";
			publishList.append(s);
		}
	}	
}

var selectClass = function(value) {
	var selectedOption = value.options[value.selectedIndex];
	getLessons(selectedOption.value);
}

var getClassList=function(){
	var json={
			courseId:data.data.courseId,
			classType:"CLASS"
	}
	$.get(CONTEXT_PATH+"class/ajaxListData", {
		data : JSON.stringify(json)
	}, function(data) {
		if(data.status=="success"){
			initClassList(data.data.list);
		}else{
			alert(data.message);
		}
	});
}

var initClassList= function(list){
	classs=list;
	var classList=$("select[id='classList']");
	classList.empty();
	for(var i=0;i<list.length;i++){
		if(i==0){
			classList.append("<option selected=\"selected\" value='"+list[i].id+"'>"+list[i].name+"</option>");
			getLessons(list[i].id);
		}else{
			classList.append("<option value='"+list[i].id+"'>"+list[i].name+"</option>");
		}
	}
}

var getLessons= function(classId){
	if(isEmpty(classId)){
		classId =$("#classList option:selected").val();
	}
	var json={
			classId:classId,
			lessonType:"THEORY",
			rid:data.data.rid
	}
	$.get(CONTEXT_PATH+"lessonResource/assignLesson", {
		data : JSON.stringify(json)
	}, function(data) {
		if(data.status=="success"){
			initLesson(data.data.lessons);
		}else{
			alert(data.message);
		}
	});
}
var initLesson = function(list){
	lessons = list;
	var begin = 0;
	var end = 5;
	var pageNow = 1;
	var pageNum;
	if(list.length<5) {
		end = list.length;
		pageNum = 1;
		pageNow = 1;
	} else {
		pageNum = Math.ceil(list.length/5);
		begin = (pageNow-1)*5;
	}
	var pageNow1 = $("#pageNow").text();
	$("#lastPage").click(function() {
		if(pageNow1 == 1 || pageNow ==1 ) {
			pageNow = 1;
			begin = 0;
			layer.msg('已经是第一页啦！');
		} else {
			pageNow--;
			begin = (pageNow-1)*5;
		}
		end = pageNow * 5;
		$("#pageNow").text(pageNow);
		$("#pageNum").text(pageNum);
		pageList(list,begin,end);
	});
	$("#nextPage").click(function() {
		if(pageNow1 == pageNum || pageNow == pageNum) {
			pageNow = pageNum;
			end = list.length;
			layer.msg('已经是最后一页啦！');
		} else {
			pageNow++;
			if(pageNow == pageNum) {
				end = list.length;
			} else {
				end = pageNow*5;
			}
		}
		begin = (pageNow-1)*5;
		$("#pageNow").text(pageNow);
		$("#pageNum").text(pageNum);
		pageList(list,begin,end);
	});
	$("#pageNow").text(pageNow);
	$("#pageNum").text(pageNum);
	pageList(list,begin,end);
}

var pageList = function(list,begin,end) {
	var lessonList=$("#lessonList");
	lessonList.empty();
	for(var i=begin;i<end;i++){
		var s="";
		if(contain(list[i])){
			s+="<li>";
			if(list[i].location == null){
				s+="<span title=\""+list[i].beginDate+"--"+list[i].endDate+" "+"上课地点未知"+"\">"+list[i].beginDate+"--"+list[i].endDate+" "+"上课地点未知"+"</span>";
			}else{
				s+="<span title=\""+list[i].beginDate+"--"+list[i].endDate+" 上课地点:"+list[i].location+"\">"+list[i].beginDate+"--"+list[i].endDate+" 上课地点:"+list[i].location+"</span>";
			}		
			s+="<input type=\"button\" disabled=\"disabled\" class=\"btn btn-primary add-lesson\" value=\"添加\">";
			s+="</li>";
			lessonList.append(s);
		}else{
			s+="<li>";
			if(list[i].location == null){
				s+="<span title=\""+list[i].beginDate+"--"+list[i].endDate+" "+"上课地点未知"+"\">"+list[i].beginDate+"--"+list[i].endDate+" "+"上课地点未知"+"</span>";
			}else{
				s+="<span title=\""+list[i].beginDate+"--"+list[i].endDate+" 上课地点:"+list[i].location+"\">"+list[i].beginDate+"--"+list[i].endDate+" 上课地点:"+list[i].location+"</span>";
			}
			s+="<input type=\"button\" class=\"btn btn-primary add-lesson\" onclick=\"initChooseList("+i+",this)\" value=\"添加\">";
			s+="</li>";
			lessonList.append(s);
		}
	}
}
var initChooseList = function(i,obj){
	var chooseList = $("#chooseList");
	var item=lessons[i];
	
	var s="";
	s+="<li>";
	if(item.location == null){
		s+="<span  title=\""+item.beginDate+"--"+item.endDate+" "+"上课地点未知"+"\">"+item.beginDate+"--"+item.endDate+" "+"上课地点未知"+"</span>";
	}else{
		s+="<span  title=\""+item.beginDate+"--"+item.endDate+" 上课地点:"+item.location+"\">"+item.beginDate+"--"+item.endDate+" 上课地点:"+item.location+"</span>";
	}
	s+="<input type=\"button\" class=\"btn btn-danger del-lesson\" onclick=\"deleteChooseLesson("+i+",this)\" value=\"删除\">";
	s+="</li>";
	chooseList.append(s);
	chooses.push(item);
 	$(obj).attr("disabled","disabled");
}
var deleteChooseLesson = function(i,obj) {
	var item = lessons[i];
	$(obj).parent("li").remove();
	chooses.pop(item);
	initLesson(lessons);
}
var contain = function(item){
	for(var i=0;i<chooses.length;i++){
		if(chooses[i].id==item.id){
			return true;
		}
	}
	return false;
}
var isEmpty=function(s){
	if(s==null||s=="" || s==undefined){
		return true;
	}
	return false;
};
var publishResource= function(){
	 
	 var lessons=[];
	 if(chooses.length<=0){
		 alert("请选择待发布课堂");
		 return ;
	 }
	 
	 for(var i=0;i<chooses.length;i++){
		 lessons.push(chooses[i].id);
	}
	 
	var json={
			rid:data.data.rid,
			lessons:lessons
	};
	$.post(CONTEXT_PATH+"lessonResource/ajaxBatchCreate", {
		data : JSON.stringify(json)
	}, function(data) {
		if(data.status=="success"){
			window.location.reload();
		}else{
			alert(data.message);
		}
	});
};

var deleteAssign = function(id){
	$.post(CONTEXT_PATH+"lessonResource/ajaxDelete", {
		id : id
	}, function(data) {
		if(data.status=="success"){
			window.location.reload();
		}else{
			alert(data.message);
		}
	});
}
</script>
</html>